<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\Code;

class SettingController extends Controller
{

    public $config = 'system';
    public $configPath = 'system.php';
    public $kvp = [
        [
            'title' => '用户session过期时间(分钟)',
            'key' => 'user_session_lefttime',
            'type' => 'integer',
        ],
        [
            'title' => '用户缓存过期时间(分钟)',
            'key' => 'user_cache_lefttime',
            'type' => 'integer',
        ],
        [
            'title' => '创建用户初始密码',
            'key' => 'user_default_password',
            'type' => 'string',
        ],
        [
            'title' => 'Passport ID',
            'key' => 'passport_client_id',
            'type' => 'integer',
        ],
        [
            'title' => 'Passport 密钥',
            'key' => 'passport_client_secret',
            'type' => 'string',
        ],
        [
            'title' => '开启日志监控',
            'key' => 'action_log_enabled',
            'type' => 'boolean',
        ],
        [
            'title' => '日志监控范围',
            'key' => 'action_log_scope',
            'type' => 'array',
        ],
        [
            'title' => '短信签名',
            'key' => 'sms_sign_name',
            'type' => 'string',
        ],
        [
            'title' => '短信访问键ID',
            'key' => 'sms_access_key_id',
            'type' => 'string',
        ],
        [
            'title' => '短信访问键密钥',
            'key' => 'sms_access_key_secret',
            'type' => 'string',
        ],
    ];

    public function getSettingList()
    {
        try {
            $config = config($this->config);

            if (!is_array($config)) {
                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                return $this->result;
            }

            foreach ($this->kvp as &$item) {
                if (isset($config[$item['key']])) {
                    $item['value'] = $config[$item['key']];
                } else {
                    $item['value'] = '';
                }
            }

            $this->result['data'] = $this->kvp;

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function editSetting(Request $request)
    {
        try {
            $input = $request->input();

            if (count($input) === 1) {
                $keys = array_column($this->kvp, 'key');

                foreach ($input as $key => $value) {
                    if (in_array($key, $keys)) {
                        $file = config_path($this->configPath);

                        $oldValue = config('system.' . $key);
                        if (is_array($oldValue)) {
                            $oldStr = "[";
                            foreach ($oldValue as $k => $v) {
                                $oldStr .= "'" . $v . "'," . ' ';
                            }
                            $oldStr = rtrim($oldStr, ' ');
                            $oldStr = rtrim($oldStr, ',');
                            $oldStr .= ']';
                            $oldStr = "'" . $key . "' => " . $oldStr;

                            $newArr = json_decode($value, true);
                            if (is_array($newArr) && count($newArr) > 0) {
                                $newStr = "[";
                                foreach ($newArr as $k => $v) {
                                    $newStr .= "'" . $v . "'," . ' ';
                                }
                                $newStr = rtrim($newStr, ' ');
                                $newStr = rtrim($newStr, ',');
                                $newStr .= ']';
                                $newStr = "'" . $key . "' => " . $newStr;
                            } else {
                                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                            }
                        } else if (is_int($oldValue)) {
                            $oldStr = "'" . $key . "' => " . $oldValue;

                            if (is_int($value)) {
                                $newStr = "'" . $key . "' => " . $value;
                            } else {
                                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                            }
                        } else if ($oldValue === true) {
                            $oldStr = "'" . $key . "' => true";

                            if ($value === true) {
                                $newStr = "'" . $key . "' => true";
                            } else if ($value === false) {
                                $newStr = "'" . $key . "' => false";
                            } else {
                                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                            }
                        } else if ($oldValue === false) {
                            $oldStr = "'" . $key . "' => false";

                            if ($value === true) {
                                $newStr = "'" . $key . "' => true";
                            } else if ($value === false) {
                                $newStr = "'" . $key . "' => false";
                            } else {
                                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                            }
                        } else if (is_string($oldValue)) {
                            $oldStr = "'" . $key . "' => '" . $oldValue . "'";

                            if (is_string($value)) {
                                $newStr = "'" . $key . "' => '" . $value . "'";
                            } else {
                                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                            }
                        } else {
                            $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                        }

                        file_put_contents($file, str_replace($oldStr, $newStr, file_get_contents($file)));
                    } else {
                        $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                    }
                }
            } else {
                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
            }
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

}
